package com.happymmall.controller.shipping;

import com.alibaba.fastjson.JSONObject;
import com.happymmall.entity.Result;
import com.happymmall.entity.Shipping;
import com.happymmall.service.ShippingService;
import com.happymmall.service.impl.ShippingServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/shipping/add.do")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理编码问题
        req.setCharacterEncoding("UTF-8");


        Shipping shipping = new Shipping();
        Integer userId = (Integer) req.getAttribute("userId");
        shipping.setUserId(userId);
        shipping.setReceiverName(req.getParameter("receiverName"));
        shipping.setReceiverPhone(req.getParameter("receiverPhone"));
        shipping.setReceiverMobile(req.getParameter("receiverMobile"));
        shipping.setReceiverProvince(req.getParameter("receiverProvince"));
        shipping.setReceiverCity(req.getParameter("receiverCity"));
        shipping.setReceiverAddress(req.getParameter("receiverAddress"));
        shipping.setReceiverZip(req.getParameter("receiverZip"));

        ShippingService shippingService = new ShippingServiceImpl();
        int rs = shippingService.addShipping(shipping);

        if (rs > 0) {
            resp.setContentType("application/json;charset=utf-8");
            Result result = Result.success(shipping.getId(), "新建地址成功");
            JSONObject jsonObject = new JSONObject();
            String info = jsonObject.toJSONString(result);
            PrintWriter out = resp.getWriter();
            out.write(info);
        } else {
            Result.success("更新地址失败");
        }

    }
}
